package com.situ.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component
public class SpringRabbitListener {

    /*@RabbitListener(queues = "basic.queue")
    public void listenBasicQueue(String msg) {
        System.out.println("消费者收到basic.queue的消息：" + msg);
    }*/

    @RabbitListener(queues = "basic.queue")
    public void listenBasicQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "basic.queue")
    public void listenBasicQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanout.queue1"),
            exchange = @Exchange(name = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    public void  listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到fanout.queue1消息： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanout.queue2"),
            exchange = @Exchange(name = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    public void  listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到fanout.queue2消息： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue1"),
            exchange = @Exchange(name = "directExchange", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void  listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1消息： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange(name = "directExchange", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void  listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到direct.queue2消息： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者接收到topic.queue1的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue2"),
            exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "japan.#"
    ))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者接收到topic.queue2的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue3"),
            exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "#.weather"
    ))
    public void listenTopicQueue3(String msg) {
        System.out.println("消费者接收到topic.queue3的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue4"),
            exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue4(String msg) {
        System.out.println("消费者接收到topic.queue4的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("delay.queue"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"), //type类型是：x-delayed-message
            key = "delay"
    ))
    public void listenDelayMessage(String msg) {
        System.out.println("接受延时消息: " + msg);
    }



}
